package cn.stylefeng.guns.sys.modular.system.service;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.guns.sys.modular.system.db4.mapper.BizShopMapper;
import cn.stylefeng.guns.sys.modular.system.db4.mapper.TeamMapper;
import cn.stylefeng.guns.sys.modular.system.entity.MeiTuan;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyWxMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.MeiTuanMapper;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MeiTuanService extends ServiceImpl<MeiTuanMapper, MeiTuan> {

    @Autowired
    private BizShopMapper bizShopMapper;
    @Autowired
    private BtyGufenzhuanrangService gufenzhuanrangService;
    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private BtyWxMapper btyWxMapper;

    /**
     * 判断是否存在此妖号
     */
    public String judge(String yaoMa) {
        return this.baseMapper.judge(yaoMa);
    }

    /**
     * 新增数据
     */
    public int insertMeiTuan(MeiTuan meiTuan) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        meiTuan.setSj(sdf.format(new Date()));
        meiTuan.setPt("1");
        return this.baseMapper.insert(meiTuan);
    }
    public Page<Map<String, Object>> list(String condition, String sjh,String ym,String beginTime,String endTime,String xf,String sex, String xm) {
        Page page = LayuiPageFactory.defaultPage();

        return this.baseMapper.list(page, condition, sjh, ym, beginTime, endTime, xf, sex, xm);
    }
    public List<Map<String, Object>> listExport(String condition, String sjh, String ym, String beginTime, String endTime, String xf, String sex) {

        return this.baseMapper.listExport(condition,sjh,ym,beginTime,endTime,xf,sex);
    }

    /**
     * 查询店名列表(店长)
     * @param account
     * @return
     */
    public List<Map> dmList(String account) {

        List<Map> mapList = new ArrayList<>();

        if (ToolUtil.isNotEmpty(account)){

            mapList = bizShopMapper.dmList(account);
        }

        return mapList;
    }

    /**
     * 录入美团信息
     * @param meiTuan
     * @param account
     */
    public void add(MeiTuan meiTuan, String account) {

        if (ToolUtil.isNotEmpty(account)){
            //查询录入店的店长
            String phone = bizShopMapper.getDz(meiTuan.getMm());
           //店长可录入信息
            if (phone.equals(account)){

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                meiTuan.setSj(sdf.format(new Date()));
                meiTuan.setPt("1");
                this.baseMapper.insert(meiTuan);
            }

        }
    }

    /**
     * 美团列表
     * @param meiTuan
     * @param account
     * @return
     */
    public LayuiPageInfo mtList(MeiTuan meiTuan, String account) {
        if (ToolUtil.isNotEmpty(account)){

            Page pageContext = LayuiPageFactory.defaultPage();
//            IPage page = this.baseMapper.customPageList(pageContext, meiTuan);
//            return LayuiPageFactory.createPageInfo(page);

            //获取用户角色名称
            List<String> list = gufenzhuanrangService.roleList(account);
            List<String> roleList = null;
            IPage page = null;

            if (list.contains("超级管理员")){
                page = this.baseMapper.mtList(pageContext, meiTuan, null);

            }else if (list.contains("营运经理")){

                //查询营运经理所有的店
                roleList = teamMapper.getYyYm(account);

                page = this.baseMapper.mtList(pageContext, meiTuan, roleList);
            }else if (list.contains("督导")){

                //查询督导所有的店
                roleList = teamMapper.getDdYm(account);
                page = this.baseMapper.mtList(pageContext, meiTuan, roleList);

                //店长
            }else if (list.contains("店长报销单管理")){
                //查询店长所有的店
                roleList = teamMapper.getDzYm(account);
                page = this.baseMapper.mtList(pageContext, meiTuan, roleList);

            }

            if (page == null){
                return new LayuiPageInfo();
            }else {
                return LayuiPageFactory.createPageInfo(page);
            }
        }
        return null;
    }

    /**
     * 美团列表门店详情
     * @param meiTuan
     * @return
     */
//    public List<Map> getByMm(MeiTuan meiTuan, Integer sta) {
    public LayuiPageInfo getByMm(MeiTuan meiTuan, Integer sta) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //当前日期
        Date date = new Date();
        String dateSdf = sdf.format(date);
        String beginTime = null;
        String endTime = null;

        if (ToolUtil.isNotEmpty(sta)){
            try {
                endTime = DateUtil.addDate(dateSdf, 1);
                //近半月
                if (sta == 0){
                    beginTime = DateUtil.addDate(dateSdf, -15);
                    //近一月
                }else if (sta == 1){
                    beginTime = DateUtil.addMonth(dateSdf, -1);

                    //近半年
                }else if (sta == 2){
                    beginTime = DateUtil.addMonth(dateSdf, -6);

                    //近一年
                }else if (sta == 3){
                    beginTime = DateUtil.addYear(dateSdf, -1);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

//        List<Map> list = this.baseMapper.getByMm(meiTuan, beginTime, dateSdf);
        Page pageContext = LayuiPageFactory.defaultPage();
        IPage page = this.baseMapper.getByMm(pageContext, meiTuan, beginTime, endTime);
//        return list;
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 判断当前登录者是否是店长
     * @param account
     * @return
     */
//    public Map getRole(String account) {
//
//        Map mapList = new HashMap<>();
//        int status = 0;//0不是店长,1店长
//
//        if (ToolUtil.isNotEmpty(account)){
//            String roleIds = null;
//
//            //根据phone查询当前登陆者角色
//            Map map = btyWxMapper.getRole(account);
//            if (map != null && map.size() != 0) {
//                roleIds = map.get("roleId").toString();//获取当前登录者角色
//            }
//            //店长
//            if (roleIds.contains("1293016774334623746")){
//
//                //查询是否有店长为此人的店
//                int con = bizShopMapper.dmCon(account);
//                 if (con > 0){
//                    status = 1;
//                }
//            }
//        }
//        mapList.put("status", status);
//        return mapList;
//    }

    public Map getRole(String account) {

        Map mapList = new HashMap<>();
        int status = 0;//0不是店长,1店长

        if (ToolUtil.isNotEmpty(account)){
            String rankName = null;

            //根据phone查询当前登陆者角色
            Map map = bizShopMapper.getRole(account);

            if (map != null && map.size() != 0) {
                rankName = map.get("rankName").toString();//获取当前登录者角色
            }
            //店长
            if (ToolUtil.isNotEmpty(rankName) && rankName.contains("店长")){

                //查询是否有店长为此人的店
                int con = bizShopMapper.dmCon(account);
                if (con > 0){
                    status = 1;
                }
            }
        }
        mapList.put("status", status);
        return mapList;
    }

}
